<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="query">
				<el-form-item>
					<el-input v-model="query.keyword" placeholder="请输入关键字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="search">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="pageData.rows" highlight-current-row v-loading="listLoading" @selection-change="selsChange"
			style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="name" label="部门名" width="100" sortable>
			</el-table-column>
			<el-table-column prop="intro" label="简介" width="200" sortable>
			</el-table-column>
			<el-table-column prop="create_time" label="创建时间" width="180" sortable>
			</el-table-column>
			<el-table-column prop="update_time" label="更新时间" width="180" sortable>
			</el-table-column>
			<el-table-column prop="manager.username" label="部门经理" width="120" sortable>
			</el-table-column>
			<el-table-column prop="parent.name" label="上级部门" min-width="120" sortable>
			</el-table-column>
			<el-table-column label="操作" width="150">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length === 0">批量删除</el-button>
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="query.pageSize"
				:total="pageData.total" style="float:right;">
			</el-pagination>
		</el-col>

		<!--新增或修改-->
		<el-dialog title="新增或修改" :visible.sync="departmentFormVisible" :close-on-click-modal="false">
			<el-form :model="department" label-width="80px" :rules="departmentFormRules" ref="departmentForm">
				<el-form-item label="名称" prop="name">
					<el-input v-model="department.name" auto-complete="off"></el-input>
				</el-form-item>

				<el-form-item label="简介" prop="intro">
					<el-input type="textarea" v-model="department.intro"></el-input>
				</el-form-item>

				<el-form-item label="部门经理" prop="manager">
					<!-- value-key="id" 必须写 -->
					<el-select v-model="department.manager" clearable value-key="id" placeholder="请选择">
						<!--下拉选项
						  :key="" 唯一标识
						  :label 选择之后展示到选择框中的值
						  :value 选中之后绑定给模型层的值 
						-->
						<el-option v-for="item in employees" :key="item.id" :label="item.username" :value="item">
							<span style="float: left">{{ item.username }}</span>
							<span style="float: right; color: #8492a6; font-size: 13px">{{ item.email }}</span>
						</el-option>
					</el-select>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="departmentFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="handleSave" :loading="departmentLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
import util from '../../common/js/util'

import { getUserListPage, removeUser, batchRemoveUser, editUser, addUser } from '../../api/api';

export default {
	data() {
		return {
			query: {
				currentPage: 1,
				pageSize: 10,
				keyword: ''
			},
			pageData: {
				total: 0,
				rows: []
			},
			listLoading: false,
			sels: [],//列表选中列

			departmentFormVisible: false,//编辑界面是否显示
			departmentLoading: false,
			departmentFormRules: {
				name: [
					{ required: true, message: '请输入名称', trigger: 'blur' }
				]
			},
			//新增界面数据
			department: {
				id: null,
				name: '',
				intro: '',
				parent: {},
				manager: {},
			},
			// 所有的员工，用以选择部门经理
			employees: [],
			// 部门树，用以选择上级部门
			departmentTree: {}
		}
	},
	methods: {
		//获取部门页面
		getDepartments() {
			this.listLoading = true; //发起请求之前显示加载条
			//发起请求获取数据
			this.$http.post("/department", this.query)
				.then(result => {
					result = result.data; //{success,result:{}}
					if (result.success) {
						this.pageData = result.data;
					} else {
						this.$message({
							message: '获取分页数据失败！' + result.message,
							type: 'error'
						});
					}
					this.listLoading = false;
				})
				.catch(result => {
					this.$message({
						message: '系统错误！',
						type: 'error'
					});
				})
		},

		// 高级查询
		search() {
			// 回到第一页
			this.query.currentPage = 1;
			this.getDepartments();
		},

		// 添加
		handleAdd: function () {
			this.departmentFormVisible = true;
			this.department = {
				name: '',
				intro: '',
				manager: {},
				parent: {}
			};
			this.getEmployees();
		},
		// 删除
		handleDel: function (index, row) {
			this.$confirm('确认删除该记录吗?', '提示', {
				type: 'warning'
			}).then(() => {
				this.listLoading = true;
				let id = row.id;
				console.log(id);
				this.$http.delete("/department/" + id)
					.then((result) => {
						this.listLoading = false;
						if (result.data.success) {
							this.$message({
								message: '删除成功',
								type: 'success'
							});
							this.getDepartments();
						} else {
							this.$message({
								message: '删除失败',
								type: 'error'
							});
						}

					})
					.catch(result => {
						this.$message({
							message: '系统异常',
							type: 'error'
						});
					});
			});
		},

		// 修改
		handleEdit: function (index, row) {
			this.departmentFormVisible = true;
			this.department = Object.assign({}, row);
		},

		// 获取所有的员工
		getEmployees() {
			this.$http.get("/employee")
				.then(result => {
					result = result.data;
					if (result.success) {
						this.employees = result.data;
					} else {
						this.$message({
							message: '获取所有员工失败！' + result.message,
							type: 'error'
						});
					}
					this.listLoading = false;
				})
				.catch(result => {
					this.$message({
						message: '系统错误！',
						type: 'error'
					});
				})
		},

		// 处理新增或修改后的保存
		handleSave() {
			this.$refs.departmentForm.validate((valid) => {
				if (valid) {
					this.$confirm('确认提交吗？', '提示', {}).then(() => {
						this.departmentLoading = true;
						let para = Object.assign({}, this.department);
						this.$http.put("/department", para)
							.then(result => {
								result = result.data;
								if (result.success) {
									this.$message({
										message: '保存成功',
										type: 'success'
									});
									//关闭对话框
									this.departmentFormVisible = false;
									//重置表单
									this.$refs['departmentForm'].resetFields();
									//重新加载数据
									this.getDepartments();
								} else {
									this.$message({
										message: '保存失败',
										type: 'error'
									});
								}
								this.departmentLoading = false;
							})
					})
				}
			})
		},

		// 批量删除选择id
		selsChange: function (sels) {
			this.sels = sels;
		},

		// 页码选择改变
		handleCurrentChange(val) {
			this.query.currentPage = val;
			this.getDepartments();
		},
		//批量删除
		batchRemove: function () {
			let ids = this.sels.map(item => item.id);
			console.log(ids);
			this.$confirm('确认删除选中记录吗？', '提示', {
				type: 'warning'
			}).then(() => {
				this.listLoading = true;
				this.$http.patch("/department", ids)
				.then((result) => {
					this.listLoading = false;
					this.$message({
						message: '删除成功',
						type: 'success'
					});
					this.getDepartments();
				})
				.catch(result => {
					this.listLoading = false;
					this.$message({
						message: '删除失败',
						type: 'error'
					});
				});
			})
		}
	},

	mounted() {
		this.getDepartments();
	}
}

</script>

<style scoped>
</style>